import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

class staff {

	public staff() {
		try {
			Showlist.adminList();
			System.out.println("1. Create customer or facilities or Venue");
			System.out.println("2. Edit customer or facilities");
			System.out.println("3. Delete customer or facilities or venue");
			System.out.println("4. Retrieve administrator or staff");
			System.out.println("5. Active non uni customer or facilities");
			System.out.println("6. Deactivate non uni customer or facilities");
			System.out.println("7. Reserve facility");
			System.out.println("8. Delete reserved facility");
			System.out.println("9. Change password");
			System.out.println("10. Backup data");
			System.out.println("11. Exit");
			System.out.println("------------------------------------");

			int choice = 0;

			int bookingid;
			int venueid;
			int rid;
			String temp;
			Date d = new Date();
			Date nextyear = new Date();
			nextyear.setTime(d.getTime() + 1 * 24 * 60 * 60 * 1000);
			Date bookingdate = null;
			Date bookingstart;
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long diff;
			long diffDays = 0;
			String username;
			String password;
			String firstname;
			String lastname;
			String facilityname;
			String role;
			boolean license;
			boolean status;
			int uid;
			int fid;
			String venuename;
			int venuecap;
			Scanner input = new Scanner(System.in);
			System.out.println("choice:");
			choice = input.nextInt();

			while (choice != 12) {
				switch (choice) {
				case 1:
					System.out.println("------------------------------------");
					System.out.println("Create customer or facilities");
					System.out.println("1.Customer: ");
					System.out.println("2.Facility: ");
					System.out.println("3.Venue: ");
					System.out.println("4.Last page: ");
					System.out.println("choice:");
					System.out.println("------------------------------------");

					choice = input.nextInt();

					while (choice != 4) {
						switch (choice) {
						case 1:
							System.out.println("Please enter username: ");
							username = input.next();
							if (Check.checkUsername(username)) {// check
								// facilityid
								System.out.println("username already exist!");
								break;
							} else {
								System.out.println("Please enter password: ");
								password = input.next();
								while (!PasswordCheck.checkPassword(password)) {
									System.out
											.println("A valid password must contain at least 8 characters and must include at least a number and a letter. ");
									System.out
											.println("Please re-enter new password: ");
									System.out
											.println("------------------------------------");

									password = input.next();
								}
								System.out.println("Please enter first name: ");
								firstname = input.next();
								System.out.println("Please enter last name: ");
								lastname = input.next();
								System.out
										.println("Please enter user type(): ");

								role = input.next();

								/*
								 * if (role.equals("NONUNI")) { User user = new
								 * User(username, password, firstname, lastname,
								 * role, false, nextyear); user.insert();
								 * System.out
								 * .println("Create User successful");
								 * 
								 * } else { while (!role.equals("CUSTOMER")) {
								 * System.out .println(
								 * "Staff can only create NONUNI or CUSTOMER! Please re-enter: "
								 * ); role = input.next();
								 * 
								 * }
								 */
								User user = new User(username, password,
										firstname, lastname, role, false, null);
								user.insert();
								System.out.println("Create User successful");

							}
							break;
						case 2:
							System.out.println("Please enter facility name: ");
							facilityname = input.next();
							if (Check.checkFacility(facilityname)) {// check
																	// //
																	// facilityid
								System.out.println("facility already exist!");
								break;
							} else {
								Facility f = new Facility(facilityname, true);
								f.insert();
								System.out
										.println("Create Facility successful");
							}
							break;
						case 3:
							Showlist.facilityList();
							System.out.println("Please enter facility id: ");
							fid = input.nextInt();
							System.out.println("Please enter venue name: ");
							venuename = input.next();
							if (Check.checkVenue(venuename)) {// check //
																// facilityid
								System.out.println("Venue already exist!");
								break;
							} else {
								System.out
										.println("Please enter venue capacity: ");
								venuecap = input.nextInt();
								System.out
										.println("Does the venue required licence?(1.True 2.false) ");
								license = license = input.next() != null;
								Venue v = new Venue(venuename, fid, venuecap,
										license, true);
								v.insert();
								System.out.println("Create Venue successful! ");
								System.out
										.println("Add more venue?(1.Yes 2.No)");
								choice = input.nextInt();
								while (choice == 1) {
									System.out
											.println("Please enter venue name: ");
									venuename = input.next();
									System.out
											.println("Please enter venue capacity: ");
									venuecap = input.nextInt();
									System.out
											.println("Does the venue required licence?(1.True 2.false) ");
									license = license = input.next() != null;
									v.setVenueName(venuename);
									v.setFacilityId(fid);
									v.setCapacity(venuecap);
									v.setRequirelic(license);
									v.setStatus(true);
									v.insert();
									System.out
											.println("Create Venue successful! ");
									System.out
											.println("Add more venue?(1.Yes 2.No)");
									choice = input.nextInt();
								}
							}
							break;
						default:
							System.out
									.println("Invaild Input, please enter 1 or 2.");
							System.out
									.println("------------------------------------");
							break;

						}
						System.out
								.println("------------------------------------");
						System.out.println("Create customer or facilities");
						System.out.println("1.Customer: ");
						System.out.println("2.Facility: ");
						System.out.println("3.Venue: ");
						System.out.println("4.Last page: ");
						System.out.println("choice:");
						System.out
								.println("------------------------------------");
						choice = input.nextInt();
					}
					break;
				case 2:
					System.out.println("------------------------------------");
					System.out.println("Edit customer or facilities");
					System.out.println("1.Customer: ");
					System.out.println("2.Facility: ");
					System.out.println("3.Last page: ");
					System.out.println("choice:");
					System.out.println("------------------------------------");

					choice = input.nextInt();

					while (choice != 3) {
						switch (choice) {
						case 1:
							System.out.println("Please enter username: ");
							username = input.next();
							while (!Check.checkUsername(username)) {// check
																	// username
																	// should
																	// exist
								System.out
										.println("username does not exist! Please Re-enter: ");
								username = input.next();
							}
							System.out.println("Please enter password: ");
							password = input.next();
							while (!PasswordCheck.checkPassword(password)) {
								System.out
										.println("A valid password must contain at least 8 characters and must include at least a number and a letter. ");
								System.out
										.println("Please re-enter new password: ");
								System.out
										.println("------------------------------------");

								password = input.next();
							}

							// User.setPassword(password);
							System.out.println("Please enter first name: ");
							firstname = input.next();
							// User.setRole(firstname);
							System.out.println("Please enter last name: ");
							lastname = input.next();
							System.out
									.println("Please enter user hold license or not(true or false): ");
							license = input.next() != null;
							System.out.println("Please enter user type: ");
							role = input.next();

							System.out.println("Please enter user status: ");
							status = input.next() != null;

							if (role.equals("CUSTOMER")) {
								User u = new User(username, password,
										firstname, lastname, role, license,
										null);
								u.setStatus(status);
							} else {
								System.out
										.println("Please enter expire date: ");
								temp = input.next();// checkdate!!!!!!!
								try {

									bookingdate = df.parse(temp);

								} catch (ParseException e) {
									System.out.println("Unable to parse "
											+ temp);
								}
								System.out.println(bookingdate);
								User u = new User(username, password,
										firstname, lastname, role, license,
										bookingdate);
								u.setStatus(status);
							}

							User.update();
							System.out.println("Update User successful");

							break;
						case 2:
							Showlist.facilityList();
							System.out.println("Please enter facility id: ");
							fid = input.nextInt();
							if (!Check.checkFacilityID(fid)) {// check
																// facilityid
																// should
																// exist
								System.out
										.println("facility dose not already exist!");
							} else {
								Facility fa = new Facility(fid);
								System.out
										.println("Please enter facility name: ");
								System.out
										.println("------------------------------------");

								facilityname = input.next();
								Facility.setFacilityName(facilityname);
								Facility.update();
								Showlist.venueList(fid);
								System.out
										.println("Update facility successful");

								System.out.println("Please enter venue name: ");
								venuename = input.next();
								System.out
										.println("Please enter venue capacity: ");
								venuecap = input.nextInt();
								System.out
										.println("Does the venue required licence?(1.True 2.false) ");
								license = license = input.next() != null;
								Venue v = new Venue(venuename,
										fa.getFacilityId(), venuecap, license,
										true);
								v.update();
								System.out.println("Update Venue successful! ");
								System.out
										.println("Update more venue?(1.Yes 2.No)");
								choice = input.nextInt();
								while (choice == 1) {
									System.out
											.println("Please enter venue name: ");
									venuename = input.next();
									System.out
											.println("Please enter venue capacity: ");
									venuecap = input.nextInt();
									System.out
											.println("Does the venue required licence?(1.True 2.false) ");
									license = license = input.next() != null;
									v.setVenueName(venuename);
									v.setFacilityId(fa.getFacilityId());
									v.setCapacity(venuecap);
									v.setRequirelic(license);
									v.setStatus(true);
									v.insert();
									System.out
											.println("Create Venue successful! ");
									System.out
											.println("Add more venue?(1.Yes 2.No)");
									choice = input.nextInt();
								}
							}
							break;
						default:
							System.out
									.println("Invaild Input, please enter 1 or 2.");

						}

						System.out
								.println("------------------------------------");
						System.out.println("Edit customer or facilities");
						System.out.println("1.Customer: ");
						System.out.println("2.Facility: ");
						System.out.println("3.Last page: ");
						System.out.println("choice:");
						System.out
								.println("------------------------------------");
						choice = input.nextInt();
					}
					break;
				case 3:
					System.out.println("------------------------------------");
					System.out
							.println("Delete customer or facilities or venue");
					System.out.println("1.Customer: ");
					System.out.println("2.Facility: ");
					System.out.println("3.Venue: ");
					System.out.println("4.Last page: ");
					System.out.println("choice:");
					System.out.println("------------------------------------");

					choice = input.nextInt();

					while (choice != 4) {
						switch (choice) {
						case 1:
							Showlist.nonuniList();
							Showlist.studentList();
							System.out.println("Please enter UID: ");
							uid = input.nextInt();
							if (!Check.checkUserID(uid)) {// check
								// uid
								// should
								// exist
								System.out.println("user does not exist!");

							} else {
								User usr = new User(uid);
								usr.delete();
								System.out.println("Delete User successful");
							}
							break;
						case 2:
							Showlist.facilityList();
							System.out.println("Please enter FID: ");
							fid = input.nextInt();
							if (!Check.checkFacilityID(fid)) {// check
								// facilityid
								// should
								// exist
								System.out.println("facility does not exist!");
								break;
							} else {
								Facility fa2 = new Facility(fid);
								if (fa2.getStatus())// check whether active or
								// not
								{
									System.out
											.println("Facility is active can not delete");
									break;
								}
								fa2.delete();
								System.out
										.println("Delete facility successful");
							}
							break;
						case 3:
							Showlist.facilityList();
							System.out.println("Please enter FID: ");
							fid = input.nextInt();
							if (!Check.checkFacilityID(fid)) {// check
								// facilityid
								// should
								// exist
								System.out.println("facility does not exist!");
								break;
							} else {
								Showlist.venueList(fid);
								System.out.println("Please enter venue id: ");
								venueid = input.nextInt();
								if (!Check.checkVenueID(venueid, fid)) {// check
									// venue
									// should
									// exist
									System.out.println("Venue does not exist!");
									break;
								} else {
									Venue venueDelete = new Venue(venueid);
									venueDelete.delete();
									System.out
											.println("Delete facility successful");
								}
							}
							break;
						default:
							System.out
									.println("Invaild Input, please enter 1, 2,3 or 4.");
							System.out
									.println("------------------------------------");
							break;

						}
						System.out
								.println("------------------------------------");
						System.out
								.println("Delete customer or facilities or venue");
						System.out.println("1.Customer: ");
						System.out.println("2.Facility: ");
						System.out.println("3.Venue: ");
						System.out.println("4.Last page: ");
						System.out.println("choice:");
						System.out
								.println("------------------------------------");
						choice = input.nextInt();
					}
					break;
				case 4:
					System.out.println("------------------------------------");

					System.out.println(" Retrieve Customer or Facility");

					System.out.println("1.Customer or Staff: ");
					System.out.println("2.Facility: ");
					System.out.println("3.Last page: ");
					System.out.println("choice:");
					System.out.println("------------------------------------");

					choice = input.nextInt();

					while (choice != 3) {
						switch (choice) {
						case 1:
							Showlist.nonuniList();
							Showlist.studentList();
							System.out.println("Please enter UID: ");
							System.out
									.println("------------------------------------");

							uid = input.nextInt();

							if (!Check.checkUserID(uid)) {// // check
								// uid
								// should
								// exist
								System.out.println("user does not exist!");
							} else {
								User usr2 = new User(uid);
								if (usr2.getRole().equals("ADMIN")) {
									System.out.println("Could not retrieve ADMIN! ");
								} else {
									System.out.println("First name: "
											+ User.getFirstName());
									System.out.println("Last name: "
											+ User.getLastName());
									System.out.println("Type: "
											+ User.getRole());
									System.out.println("Expired date: "
											+ User.getExpired());
									System.out.println("Status: "
											+ User.getStatus());
									System.out
											.println("------------------------------------");
								}
							}
							break;
						case 2:
							Showlist.facilityList();
							System.out.println("Please enter FID: ");
							System.out
									.println("------------------------------------");

							fid = input.nextInt();
							if (!Check.checkFacilityID(fid)) {// check
								// facilityid
								// should
								// exist
								System.out.println("facility does not exist!");
								break;
							} else {
								Facility fa2 = new Facility(fid);
								System.out.println("Facility name: "
										+ Facility.getFacilityName());
								System.out.println("Status: "
										+ Facility.getStatus());
								System.out
										.println("------------------------------------");
								Showlist.venueList(fid);
							}
							break;
						default:
							System.out
									.println("Invaild Input, please enter 1 or 2.");
							System.out
									.println("------------------------------------");

						}
						System.out
								.println("------------------------------------");

						System.out.println(" Retrieve administrator or staff");

						System.out.println("1.Customer: ");
						System.out.println("2.Facility: ");
						System.out.println("3.Last page: ");
						System.out.println("choice:");
						System.out
								.println("------------------------------------");
						choice = input.nextInt();
					}
					break;

				case 5:
					System.out.println("------------------------------------");

					System.out
							.println("5. Active non uni customer or facilities");

					System.out.println("1.Customer: ");
					System.out.println("2.Facility: ");
					System.out.println("3.Last page: ");
					System.out.println("choice:");
					System.out.println("------------------------------------");

					choice = input.nextInt();

					while (choice != 3) {
						switch (choice) {
						case 1:
							Showlist.nonuniList();
							System.out.println("Please enter UID: ");
							System.out
									.println("------------------------------------");

							uid = input.nextInt();
							User usr2 = new User(uid);
							usr2.setStatus(true);
							usr2.update();
							System.out.println("Active User successful");

							break;
						case 2:
							System.out.println("Please enter FID: ");
							System.out
									.println("------------------------------------");

							fid = input.nextInt();
							Facility fa2 = new Facility(fid);
							fa2.setStatus(true);
							fa2.update();
							System.out.println("Active Facility successful");

							break;
						default:
							System.out
									.println("Invaild Input, please enter 1 or 2.");
							System.out
									.println("------------------------------------");

						}
						System.out
								.println("------------------------------------");

						System.out
								.println("5. Active non uni customer or facilities");

						System.out.println("1.Customer: ");
						System.out.println("2.Facility: ");
						System.out.println("3.Last page: ");
						System.out.println("choice:");
						System.out
								.println("------------------------------------");

						choice = input.nextInt();
					}
					break;

				case 6:
					System.out.println("------------------------------------");

					System.out
							.println(" Deactivate non uni customer or facilities");

					System.out.println("1.Customer: ");
					System.out.println("2.Facility: ");
					System.out.println("3.Last page: ");
					System.out.println("choice:");
					System.out.println("------------------------------------");

					choice = input.nextInt();

					while (choice != 3) {
						switch (choice) {
						case 1:
							Showlist.nonuniList();
							System.out.println("Please enter UID: ");
							System.out
									.println("------------------------------------");

							uid = input.nextInt();
							User usr2 = new User(uid);
							usr2.setStatus(false);
							usr2.update();
							System.out.println("Deactive User successful");
							break;
						case 2:
							Showlist.facilityList();

							System.out.println("Please enter FID: ");
							System.out
									.println("------------------------------------");

							fid = input.nextInt();
							Facility fa2 = new Facility(fid);
							fa2.setStatus(false);
							fa2.update();
							System.out.println("Deactive Facility successful");

							break;
						default:
							System.out
									.println("Invaild Input, please enter 1 or 2.");
							System.out
									.println("------------------------------------");

						}
						System.out
								.println("------------------------------------");

						System.out
								.println(" Deactivate non uni customer or facilities");

						System.out.println("1.Customer: ");
						System.out.println("2.Facility: ");
						System.out.println("3.Last page: ");
						System.out.println("choice:");
						System.out
								.println("------------------------------------");
						choice = input.nextInt();
					}
					break;

				case 7:
					Showlist.facilityList();
					System.out.println("------------------------------------");

					System.out.println("Reserve facility");

					System.out.println("Please enter facility ID: ");
					System.out.println("------------------------------------");

					fid = input.nextInt();
					if (!Check.checkFacilityID(fid)) {// check
						System.out.println("facility does not exist!");
					} else {
						Facility f = new Facility(fid);
						if (!Facility.getStatus())// can not book deactive
						{
							System.out.println("Facility is deactive ");
						} else {
							System.out.println("Please enter booking date: ");
							temp = input.next();// checkdate!!!!!!!
							try {

								bookingdate = df.parse(temp);
								// cal the different date
								diff = bookingdate.getTime() - d.getTime();
								diffDays = diff / (24 * 60 * 60 * 1000);

							} catch (ParseException e) {
								System.out.println("Unable to parse " + temp);
							}
							if (diffDays < 7) {

								System.out
										.println("Reserve must after 7 days: ");
							} else {
								// Check.checkFacility(facilityname); check
								// facility
								// with
								// date
								System.out.println("Please re-enter password ");
								System.out
										.println("------------------------------------");

								password = input.next();
								User usr = new User(Login.getUserId());
								while (!password.equals(usr.getPassword())) {
									System.out
											.println("Please re-enter correct password ");
									password = input.next();
								}
								System.out.println(bookingdate);
								Reserve re = new Reserve(fid, bookingdate);
								re.insert();
								System.out
										.println("Reserve Facility successful");
							}
						}
					}
					break;
				case 8:
					System.out.println("------------------------------------");
					System.out.println("Delete reserved facility");
					Showlist.facilityList();
					System.out.println("Please enter facility ID: ");
					fid = input.nextInt();
					while (!Check.checkFacilityID(fid)) {
						System.out
								.println("Facility does not exist! Please Facility ID:");
						fid = input.nextInt();
					}
					Showlist.reserveList(fid);
					System.out.println("Please enter reserve ID: ");
					System.out.println("------------------------------------");

					rid = input.nextInt();
					if (!Check.checkReserveID(rid)) {// check
						System.out.println("Reserve record does not exist!");
					} else {
						Reserve red = new Reserve(rid);
						System.out.println("Please re-enter password ");
						System.out
								.println("------------------------------------");

						password = input.next();
						User usr2 = new User(Login.getUserId());
						while (!password.equals(usr2.getPassword())) {
							System.out
									.println("Please re-enter correct password ");
							System.out
									.println("------------------------------------");
							password = input.next();
						}

						red.delete();
						System.out.println("Cancle Facility successful");
					}
					break;
				case 9:
					System.out.println("Change password");
					System.out.println("Please enter new password: ");
					System.out.println("------------------------------------");

					password = input.next();

					while (!PasswordCheck.checkPassword(password)) {
						System.out
								.println("A valid password must contain at least 8 characters and must include at least a number and a letter. ");
						System.out.println("Please re-enter new password: ");
						System.out
								.println("------------------------------------");

						password = input.next();
					}
					User user = new User(Login.getUserId());
					User.update();
					System.out.println("Password update successful");
					break;

				case 10:
					Backup.backupAll();
					System.out.println("Backup successful");
					break;
				case 11:
					System.out.println("Bye!");
					System.exit(0);
					break;
				default:
					System.out.println("Invaild Input, please enter 1 to 11.");
					System.out.println("------------------------------------");

				}

				System.out.println("------------------------------------");
				System.out.println("1. Create customer or facilities");
				System.out.println("2. Edit customer or facilities");
				System.out.println("3. Delete customer or facilities");
				System.out.println("4. Retrieve administrator or staff");
				System.out.println("5. Active non uni customer or facilities");
				System.out
						.println("6. Deactivate non uni customer or facilities");
				System.out.println("7. Reserve facility");
				System.out.println("8. Delete reserved facility");
				System.out.println("9. Change password");
				System.out.println("10. Backup data");
				System.out.println("11. Exit");
				System.out.println("------------------------------------");
				choice = input.nextInt();

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
